package org.leetcode.easy.leetcode58;

public class Solution
{
    public int lengthOfLastWord(String s) {

        String[] strArrays = s.split(" ");

        return strArrays[strArrays.length-1].length();

    }

    /**
     * 官方题解
     * @param s
     * @return
     */
    public int lengthOfLastWord2(String s) {

        int index = s.length()-1;

        while (s.charAt(index)==' ')
        {
            index--;
        }

        int wordLength = 0;

        while (index>=0 && s.charAt(index)!=' ')
        {
            wordLength++;
            index--;
        }

        return wordLength;

    }


        public static void main(String[] args) {
        Solution solution = new Solution();
        int i = solution.lengthOfLastWord2("a");
        System.out.println(i);
    }

}
